<template>
  <div v-if="checkPremission()" style="display: inline-block;">
    <slot></slot>
  </div>
</template>

<script>
export default {
  name: 'PremissionView',
  computed: {
    myList () {
      return this.$store.state.premission.myList
    }
  },
  props: {
    module: {
      type: String,
      required: true
    },
    action: {
      validator: value => {
        // 必须匹配下列字符串中的一个
        for (let i in value) {
          if (['CREATE', 'UPDATE', 'DELETE'].indexOf(value[i]) !== -1) {
            return true
          }
        }
        return false
      }
    }
  },
  methods: {
    checkPremission () {
      for (let i in this.action) {
        for (let j in this.myList) {
          if (
            this.myList[j].resource &&
            this.myList[j].resource.description === `${this.module}s` &&
            this.myList[j].action === this.action[i]
          ) {
            return true
          }
        }
      }
      return false
    }
  }
}
</script>

<style>
</style>
